package com.immunology.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.immunology.core.BaseDao;
import com.immunology.core.BaseTransformer;
import com.immunology.core.GenericDao;
import com.immunology.database.ConnectionManager;
import com.immunology.database.DbColumns;
import com.immunology.model.ComplaintType;
import com.immunology.model.Syndrome;
import com.immunology.service.SyndromeService;

public class ComplaintTypeDao extends BaseDao<ComplaintType> implements
		GenericDao<ComplaintType> {

	public ComplaintTypeDao(BaseTransformer<ComplaintType> transformer) {
		super(transformer, new String("COMPLAINT_TYPE"));
	}

	@Override
	public void insert(ComplaintType complaintType) {
		Connection connection = connectionManager.getConnection();
		StringBuilder query = new StringBuilder();
		query.append("INSERT INTO ");
		query.append(tableName);
		query.append(" (NAME, MULTYPLIER, SYNDROME_ID)");
		query.append(" VALUES(?, ?, ?)");
		try {
			PreparedStatement preparedStatement = (PreparedStatement) connection
					.prepareStatement(query.toString());
			preparedStatement.setString(DbColumns.FIRST_COLUMN,
					complaintType.getName());
			preparedStatement.setDouble(DbColumns.SECOND_COLUMN,
					complaintType.getMultyplier());
			preparedStatement.setInt(DbColumns.THIRD_COLUMN,
					complaintType.getSyndromeId());
			preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			connectionManager.closeConnection(connection);
		}
	}

	@Override
	public void update(int id, ComplaintType complaintType) {
		Connection connection = connectionManager.getConnection();
		StringBuilder query = new StringBuilder();
		query.append("UPDATE ");
		query.append(tableName);
		query.append(" SET NAME = ?, MULTYPLIER = ?");
		query.append(" WHERE ID = ?");
		try {
			PreparedStatement preparedStatement = (PreparedStatement) connection
					.prepareStatement(query.toString());
			preparedStatement.setString(DbColumns.FIRST_COLUMN,
					complaintType.getName());
			preparedStatement.setDouble(DbColumns.SECOND_COLUMN,
					complaintType.getMultyplier());
			preparedStatement.setInt(DbColumns.THIRD_COLUMN,
					complaintType.getId());
			preparedStatement.executeUpdate();

			preparedStatement.close();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			connectionManager.closeConnection(connection);
		}
	}

	public List<ComplaintType> getComplaintTypeListBySyndromeId(int syndromeId) {
		List<ComplaintType> complaintTypeList = new ArrayList<ComplaintType>();
		ConnectionManager connectionManager = new ConnectionManager();
		Connection connection = connectionManager.getConnection();

		Statement statement = null;
		ResultSet resultSet = null;
		try {
			statement = connection.createStatement();
			try {
				String query = "SELECT * FROM complaint_type WHERE syndrome_id = "
						+ syndromeId;
				resultSet = statement.executeQuery(query);
			} catch (Exception e) {
				System.out.println("Getting objects error");
			}
		} catch (Exception e) {
			System.out.println("Creating statement error");
		}
		int complaintTypeId = 0;
		String complaintTypeName = null;
		double complaintTypeMultyplier = 0.0;
		int syndromeId2 = 0;
		try {
			while (resultSet.next()) {
				ComplaintType complaintType = new ComplaintType();
				complaintTypeId = resultSet.getInt(DbColumns.FIRST_COLUMN);
				complaintTypeName = resultSet
						.getString(DbColumns.SECOND_COLUMN);
				complaintTypeMultyplier = resultSet
						.getDouble(DbColumns.THIRD_COLUMN);
				syndromeId2 = resultSet.getInt(DbColumns.FOURTH_COLUMN);
				complaintType.setId(complaintTypeId);
				complaintType.setName(complaintTypeName);
				complaintType.setMultyplier(complaintTypeMultyplier);
				complaintType.setSyndromeId(syndromeId2);
				complaintTypeList.add(complaintType);
			}
			statement.close();
			resultSet.close();
			connectionManager.closeConnection(connection);
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return complaintTypeList;
	}

	public Map<String, List<ComplaintType>> getComplaintTypeList() {
		Map<String, List<ComplaintType>> syndromeMap = new HashMap<String, List<ComplaintType>>();
		List<Syndrome> syndromeList = null;
		List<ComplaintType> complaintTypeList = null;

		SyndromeService syndromeService = SyndromeService.getInstance();
		syndromeList = syndromeService.getSyndromeList();

		Iterator<Syndrome> iterator = syndromeList.iterator();
		while (iterator.hasNext()) {
			Syndrome syndrome = iterator.next();
			complaintTypeList = this.getComplaintTypeListBySyndromeId(syndrome
					.getId());
			syndromeMap.put(syndrome.getName(), complaintTypeList);
		}

		return syndromeMap;
	}

}
